import { useDrag } from "@/store/modules/drag";
import container from "./container";
import example from "./example";
import formItem from "./formItem";
import tableItem from "./tableItem";
const dragStore = useDrag();
function createEditorConfig() {
  return {
    register: (component: any) => {
      let hasIt = dragStore.registerConfig.componentList.find((item) => {
        return item.key == component.key;
      });
      if (hasIt) return;
      dragStore.registerConfig.componentList.push(component);
      dragStore.registerConfig.componentMap[component.key] = component;
    }
  };
}
const registerConfig = createEditorConfig();
export function initEditor() {
  container(registerConfig);
  example(registerConfig);
  formItem(registerConfig);
  tableItem(registerConfig);
}
export let typeList = [
  { label: "表单", value: "formItem" },
  { label: "容器", value: "container" },
  { label: "表格", value: "tableItem" },
  { label: "常用示例", value: "example" }
];
export function getItem(type) {
  let nowTypeItem = typeList.find((item) => {
    return item.value == type;
  });
  return nowTypeItem;
}
export let editTypeValue = {
  high: "高级设置",
  normalProp: "常用属性",
  lessNormalProp: "较常用属性",
  prop: "属性"
};
export function cancelFn() {
  dragStore.renderData.map((item) => {
    if (item.click) item.click = false;
    if (item.childrens)
      item.childrens.map((item) => {
        if (item.click) item.click = false;
      });
  });
}
export function importJSON(doc) {
  try {
    dragStore.allReset();
    dragStore.renderData = JSON.parse(doc);
    cancelFn();
    setTimeout(() => {
      dragStore.renderData.map((item) => {
        if (item.key == "cDio") dragStore.refList[item.highEdit.ref].methods.showDialog();
        setTimeout(() => {
          item.childrens.map((ite) => {
            if (ite.type == "formItem") {
              let parent = dragStore.renderData.find((itex) => {
                return itex.id == ite.pid;
              });
              if (ite.highEdit?.itemKey) {
                let currentForm = dragStore.refList[parent.highEdit.ref].dataed.cFormRef.form;
                currentForm[ite.highEdit.itemKey] = ite.highEdit.itemValue;
              }
              ite.methods.changeRadio = (e) => {
                dragStore.refList[parent.highEdit.ref].dataed.cFormRef.form[e.key] = e.e;
              };
            }
          });
          let types = ["cDio", "cSearch", "cPage"];
          if (types.includes(item.key)) {
            item.methods.changeProps = (obj) => {
              let data = item.props[item.key];
              obj.propsName.map((ite, index) => {
                if (index != obj.propsName.length - 1) {
                  data = data[ite];
                }
              });
              data[obj.propsName.length - 1] = obj.e;
            };
          }
          if (item.highEdit.showAdd) {
            let dioRef = dragStore?.refList?.[item.highEdit.showAdd];
            if (!dioRef) return ElMessage.error("搜索组件对应的ref值的弹窗不存在");
            item.methods.showAdd = (row) => {
              dioRef.methods.showDialog(row?.row ? row?.row : {});
            };
          }
          if (item.highEdit.getListFn) {
            let cSearchKey = item.key == "cSearch" ? item.highEdit.ref : item.highEdit.cSearchKey;
            let tableRef = dragStore?.refList?.[item.highEdit.getListFn];
            if (!tableRef) return ElMessage.error("搜索组件对应的ref值的表格不存在");
            item.methods.getListFn = (fn) => {
              tableRef.getListFn(cSearchKey ? dragStore?.refList[cSearchKey].dataed.cFormRef.form : {});
              if (fn?.fn) fn.fn();
            };
          }
        }, 500);
      }, 200);
    });
  } catch (error) {
    ElMessage.error("JSON格式错误");
  }
}
